#include "EventLoop.hpp"
#include "TcpServer.hpp"
#include "ThreadPool.h"

#include <cstdlib>
#include <iostream>
#include <sys/epoll.h>
#include <unistd.h>
using std::cout;
using std::endl;


ThreadPool threadpool(4,10);

class Mytask{
public:
    Mytask(const string & msg, wd::TcpConnectionptr conn)
    :_msg(msg)
    ,_conn(conn)
    {
    }
    
    //子线程入口函数
    void process(){
        cout << "Mytask::process is running" << endl;
        _conn->sendInLoop(_msg);
    }

private:
    string _msg;
    wd::TcpConnectionptr _conn;
};


void onConnection(wd::TcpConnectionptr tcpconn){
    printf("a new clienfd connected.\n");
}

void onMessage(wd::TcpConnectionptr tcpconn){
    string msg = tcpconn->receive();
    cout << "receive msg:"<< msg << endl;
    //此处为收到客户端信息后要执行的业务处理,交给子线程处理
    Mytask task(msg,tcpconn);
    threadpool.addTask(std::bind(&Mytask::process, task));

}

void onClose(wd::TcpConnectionptr tcpconn){
    printf("a clienfd closed.\n");
}

int main()
{
    threadpool.start();
    
    wd::TcpServer tcpserver(12345,"0.0.0.0");
    tcpserver.setAllCallbacks(onConnection,onMessage,onClose);
    tcpserver.start();
    
    sleep(60);
    tcpserver.stop();

    return 0;
}

